﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Microsoft.Windows.Controls;


namespace TIMETABLEASY.EasyCalendar.Controler
{
    public partial class TimeTable : Control
    {
        static TimeTable()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(TimeTable), new FrameworkPropertyMetadata(typeof(TimeTable)));

            //CommandManager.RegisterClassCommandBinding(typeof(TimeTable), new CommandBinding(Week_selected, new ExecutedRoutedEventHandler(OnExecutedWeek_selected), new CanExecuteRoutedEventHandler(OnCanExecuteWeek_selected)));
            CommandManager.RegisterClassCommandBinding(typeof(TimeTable), new CommandBinding(NextDay, new ExecutedRoutedEventHandler(OnExecutedNextDay), new CanExecuteRoutedEventHandler(OnCanExecuteNextDay)));
            CommandManager.RegisterClassCommandBinding(typeof(TimeTable), new CommandBinding(PreviousDay, new ExecutedRoutedEventHandler(OnExecutedPreviousDay), new CanExecuteRoutedEventHandler(OnCanExecutePreviousDay))); 
        }

        #region previous/next

        public static readonly RoutedCommand NextDay = new RoutedCommand("NextDay", typeof(TimeTable));
        public static readonly RoutedCommand PreviousDay = new RoutedCommand("PreviousDay", typeof(TimeTable));
 
        private static void OnCanExecuteNextDay(object sender, CanExecuteRoutedEventArgs e)
        {
            ((TimeTable)sender).OnCanExecuteNextDay(e);
        }

        private static void OnExecutedNextDay(object sender, ExecutedRoutedEventArgs e)
        {
            ((TimeTable)sender).OnExecutedNextDay(e);
        }

        protected virtual void OnCanExecuteNextDay(CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
            e.Handled = false;
        }

        protected virtual void OnExecutedNextDay(ExecutedRoutedEventArgs e)
        {
            CalendarDay monday = this.GetTemplateChild("Monday") as CalendarDay;
            CalendarDay tuesday = this.GetTemplateChild("Tuesday") as CalendarDay;
            CalendarDay wednesday = this.GetTemplateChild("Wednesday") as CalendarDay;
            CalendarDay thursday = this.GetTemplateChild("Thursday") as CalendarDay;
            CalendarDay friday = this.GetTemplateChild("Friday") as CalendarDay;
            CalendarDay saturday = this.GetTemplateChild("Saturday") as CalendarDay;
            CalendarDay sunday = this.GetTemplateChild("Sunday") as CalendarDay;

            monday.nextWeek();
            tuesday.nextWeek();
            wednesday.nextWeek();
            thursday.nextWeek();
            friday.nextWeek();
            saturday.nextWeek();
            sunday.nextWeek();
            
            e.Handled = true;
        }

        private static void OnCanExecutePreviousDay(object sender, CanExecuteRoutedEventArgs e)
        {
            ((TimeTable)sender).OnCanExecutePreviousDay(e);
        }

        private static void OnExecutedPreviousDay(object sender, ExecutedRoutedEventArgs e)
        {
            ((TimeTable)sender).OnExecutedPreviousDay(e);
        }

        protected virtual void OnCanExecutePreviousDay(CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
            e.Handled = false;
        }

        protected virtual void OnExecutedPreviousDay(ExecutedRoutedEventArgs e)
        {
            CalendarDay monday = this.GetTemplateChild("Monday") as CalendarDay;
            CalendarDay tuesday = this.GetTemplateChild("Tuesday") as CalendarDay;
            CalendarDay wednesday = this.GetTemplateChild("Wednesday") as CalendarDay;
            CalendarDay thursday = this.GetTemplateChild("Thursday") as CalendarDay;
            CalendarDay friday = this.GetTemplateChild("Friday") as CalendarDay;
            CalendarDay saturday = this.GetTemplateChild("Saturday") as CalendarDay;
            CalendarDay sunday = this.GetTemplateChild("Sunday") as CalendarDay;

            monday.previousWeek();
            tuesday.previousWeek();
            wednesday.previousWeek();
            thursday.previousWeek();
            friday.previousWeek();
            saturday.previousWeek();
            sunday.previousWeek();

            e.Handled = true;
        }

        public void initDate()
        {
            CalendarDay monday = this.GetTemplateChild("Monday") as CalendarDay;
            CalendarDay tuesday = this.GetTemplateChild("Tuesday") as CalendarDay;
            CalendarDay wednesday = this.GetTemplateChild("Wednesday") as CalendarDay;
            CalendarDay thursday = this.GetTemplateChild("Thursday") as CalendarDay;
            CalendarDay friday = this.GetTemplateChild("Friday") as CalendarDay;
            CalendarDay saturday = this.GetTemplateChild("Saturday") as CalendarDay;
            CalendarDay sunday = this.GetTemplateChild("Sunday") as CalendarDay;

            monday.initDates();
            tuesday.initDates();
            wednesday.initDates();
            thursday.initDates();
            friday.initDates();
            saturday.initDates();
            sunday.initDates();
        }

        #endregion

        /*
        #region SelectedDatesChanges

        public static readonly RoutedCommand Week_selected = new RoutedCommand("Week_selected", typeof(TimeTable));

        private static void OnCanExecuteWeek_selected(object sender, CanExecuteRoutedEventArgs e)
        {
            ((TimeTable)sender).OnCanExecuteWeek_selected(e);
        }

        private static void OnExecutedWeek_selected(object sender, ExecutedRoutedEventArgs e)
        {
            ((TimeTable)sender).OnExecutedWeek_selected(e);
        }

        protected virtual void OnCanExecuteWeek_selected(CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
            e.Handled = false;
        }

        protected virtual void OnExecutedWeek_selected(ExecutedRoutedEventArgs e)
        {
            e.Handled = true;
            week_select_calendar_SelectedDatesChanged();
        }

        private void week_select_calendar_SelectedDatesChanged()
        {
            MessageBox.Show("No calendar selected");
        }

        #endregion*/

    }



    [TemplatePart(Type = typeof(Calendar), Name = TimeTable.CALENDAR_NAME)]
    public partial class TimeTable : Control
    {
        private const string CALENDAR_NAME = "week_select_calendar";

        private Calendar m_Calendar;

        #region Overrides Methods

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            this.m_Calendar = this.Template.FindName(CALENDAR_NAME, this) as Calendar;

            if (this.m_Calendar != null)
            {
                this.m_Calendar.SelectedDatesChanged += this.week_select_calendar_SelectedDatesChanged;
                this.m_Calendar.Loaded += this.cal_Loaded;
            }
        }

        #endregion

        private void week_select_calendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
        {
            CalendarDay day = this.GetTemplateChild(((DateTime)((Calendar)sender).SelectedDate).DayOfWeek.ToString()) as CalendarDay;
            ((Calendar)sender).DisplayDate = (DateTime) (((Calendar)sender).SelectedDate);
            jump_2_x_week(how_many_weeks(day.CurrentDate, (DateTime)((Calendar)sender).SelectedDate));
        }

        private void cal_Loaded(object sender, RoutedEventArgs e)
        {
            this.initDate();
        }

        private int how_many_weeks(DateTime currentDate, DateTime calendar_date)
        {
            TimeSpan interval = currentDate - calendar_date;
            double inter = interval.TotalDays / (double)7;
            int inter_int = Convert.ToInt32(Math.Truncate(inter));
            if (inter_int > 0)
            {
                return inter_int;
            }
            else
            {
                return inter_int;
            }
        }

        public void jump_2_x_week(int nb_week)
        {
            CalendarDay monday = this.GetTemplateChild("Monday") as CalendarDay;
            CalendarDay tuesday = this.GetTemplateChild("Tuesday") as CalendarDay;
            CalendarDay wednesday = this.GetTemplateChild("Wednesday") as CalendarDay;
            CalendarDay thursday = this.GetTemplateChild("Thursday") as CalendarDay;
            CalendarDay friday = this.GetTemplateChild("Friday") as CalendarDay;
            CalendarDay saturday = this.GetTemplateChild("Saturday") as CalendarDay;
            CalendarDay sunday = this.GetTemplateChild("Sunday") as CalendarDay;

            monday.jump_2_x_week(nb_week);
            tuesday.jump_2_x_week(nb_week);
            wednesday.jump_2_x_week(nb_week);
            thursday.jump_2_x_week(nb_week);
            friday.jump_2_x_week(nb_week);
            saturday.jump_2_x_week(nb_week);
            sunday.jump_2_x_week(nb_week);
        }
    }

}